home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / HyperCard Related / XCMDs & XFCNs / PopUpMenu 1.1 / PopUpMenu.p < prev    next >
Encoding:
Text File  |  1991-06-05  |  4.3 KB  |  184 lines  |  [TEXT/MPS ]

  1. {$R-}
  2. {$S PopUpMenu }
  3.  
  4.     
  5.     PopUpMenu(MenuItems, CheckedItem, Top, Left)
  6.     
  7.     This HyperCard external function returns the selection from a popup
  8.     menu created from a HyperCard item list (the first parameter).  The
  9.     menu is placed on the screen so that the checked item is at the
  10.     position (Top, Left) in global coordinates.
  11.     
  12.     It uses the DoPopUpMenu function from the MenuTools unit.
  13.     
  14.     It is better than most PopUpMenu implementations (he said modestly)
  15.     because it is not limited to 256 characters’ worth of menu items.
  16.  
  17. }
  18.  
  19. UNIT DummyUnit;
  20.  
  21. INTERFACE
  22.  
  23.     USES MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, HyperXCmd, MenuTools;
  24.  
  25.     PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  26.  
  27. IMPLEMENTATION
  28.  
  29.     PROCEDURE PopUpMenu(paramPtr: XCmdPtr);
  30.     FORWARD;
  31.  
  32.     PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  33.     BEGIN
  34.         PopUpMenu(paramPtr)
  35.     END { entrypoint } ;
  36.  
  37.  
  38.     PROCEDURE PopUpMenu(paramPtr: XCmdPtr);
  39.  
  40.     CONST
  41.     MenuID = 128;
  42.  
  43.     VAR
  44.     MenuItems:            Ptr;
  45.     CheckedItem:        LONGINT;
  46.     SelectedItem:        LONGINT;
  47.     Top:                LONGINT;
  48.     Left:                LONGINT;
  49.     CardWindowTop:        LONGINT;
  50.     CardWindowLeft:        LONGINT;
  51.     
  52.  
  53.         FUNCTION ParamToNum(Param: Handle): LongInt;
  54.  
  55.         VAR
  56.             Str: Str255;
  57.  
  58.         BEGIN
  59.             ZeroToPas(ParamPtr, Param^, Str);
  60.             ParamToNum := StrToNum(ParamPtr, Str);
  61.         END { ParamToNum } ;
  62.  
  63.         FUNCTION NumToParam(Num: LongInt): Handle;
  64.  
  65.         VAR
  66.             Str: Str255;
  67.  
  68.         BEGIN
  69.             NumToStr(ParamPtr, Num, Str);
  70.             NumToParam := PasToZero(ParamPtr, Str)
  71.         END { NumToParam } ;
  72.         
  73.         PROCEDURE GetCardWindowParams(VAR CardWindowTop: LONGINT; 
  74.                                         VAR CardWindowLeft: LONGINT);
  75.         VAR
  76.             WindowTopString:             Handle;
  77.             WindowLeftString:             Handle;
  78.             AString:                    Str255;
  79.         BEGIN
  80.     
  81.             WindowTopString := EvalExpr(ParamPtr, 'the top of card window');
  82.             WindowLeftString := EvalExpr(ParamPtr, 'the left of card window');
  83.             ZeroToPas(ParamPtr, WindowTopString^, AString);
  84.             CardWindowTop := StrToNum(ParamPtr, AString);
  85.     
  86.             ZeroToPas(ParamPtr, WindowLeftString^, AString);
  87.             CardWindowLeft := StrToNum(ParamPtr, AString);
  88.     
  89.             DisposHandle(WindowTopString);
  90.             DisposHandle(WindowLeftString);
  91.     
  92.         END {GetCardWindowParams} ;
  93.     
  94.         PROCEDURE AppendAllMenuItemsPtr(Menu: MenuHandle; MenuItems: Ptr);
  95.     
  96.         VAR
  97.             StartPos:                LONGINT;
  98.             EndHasBeenReached:        Boolean;
  99.             NewLength:                INTEGER;
  100.             PasMenuItems:            Str255;
  101.         BEGIN
  102.             
  103.             { The input is a Ptr string (C-style string) containing possibly 
  104.               more than 250 characters of menu items to be added to the list.
  105.               We break the input up into 250 character chunks as many times as possible,
  106.               calling AppendAllMenuItems on each chunk. }
  107.               
  108.             StartPos := 0;
  109.             EndHasBeenReached := false;
  110.             
  111.             REPEAT
  112.             
  113.                 ZeroToPas(ParamPtr, Pointer(Ord4(MenuItems) + StartPos), PasMenuItems);
  114.                 NewLength := length(PasMenuItems);
  115.                 
  116.                 IF (NewLength > 250) THEN
  117.                     BEGIN
  118.                         FOR NewLength := 250 DOWNTO 1 DO
  119.                             IF (PasMenuItems[NewLength] = ',') THEN Leave;
  120.                         NewLength := NewLength - 1;
  121.                         IF (NewLength) = 0 THEN Exit(AppendAllMenuItemsPtr);
  122.                         PasMenuItems[0] := chr(NewLength)
  123.                     END
  124.                 ELSE
  125.                     EndHasBeenReached := true;
  126.                 AppendAllMenuItems(Menu, PasMenuItems);
  127.                 StartPos := StartPos + NewLength + 1
  128.                         
  129.             UNTIL (EndHasBeenReached = true);
  130.         
  131.         END { AppendAllMenuItemsPtr } ;
  132.     
  133.         FUNCTION DoPopUpMenuPtr(MenuID: INTEGER; MenuItems: Ptr; CheckedItem: LONGINT;
  134.                             Top: LONGINT; Left: LONGINT): LONGINT;
  135.         VAR
  136.         Menu:                MenuHandle;
  137.     
  138.         BEGIN
  139.             
  140.             { Create the PopUp menu }
  141.             Menu := NewMenu(MenuID, '');
  142.             AppendAllMenuItemsPtr(Menu, MenuItems);
  143.             CheckItem(Menu, CheckedItem, true);
  144.             InsertMenu(Menu, - 1);
  145.     
  146.             { Get Menu Selection }
  147.             DoPopUpMenuPtr := PopUpMenuSelect(Menu, Top, Left, CheckedItem);
  148.     
  149.             { Tidy up }
  150.             DeleteMenu(MenuID);
  151.             DisposeMenu(Menu);
  152.     
  153.         END; {DoPopUpMenuPtr}
  154.     
  155.     BEGIN {PopUpMenu}
  156.  
  157.         WITH paramPtr^ DO
  158.         BEGIN
  159.  
  160.             { Parse parameters & Get Menu Position }
  161.             MenuItems := Params[1]^;
  162.             CheckedItem := ParamToNum(Params[2]);
  163.             GetCardWindowParams(CardWindowTop, CardWindowLeft);
  164.             Top := CardWindowTop + ParamToNum(Params[3]);
  165.             Left := CardWindowLeft + ParamToNum(Params[4]);
  166.  
  167.             { Run the popup menu }
  168.             IF (StringLength(ParamPtr, MenuItems) <> 0) THEN
  169.                 SelectedItem := DoPopUpMenuPtr(MenuID, MenuItems, CheckedItem, Top, Left)
  170.             ELSE
  171.                 SelectedItem := 0;
  172.                 
  173.             { Return the selection }
  174.             returnValue := NumToParam(LoWord(SelectedItem))
  175.  
  176.         END
  177.  
  178.     END { PopUpMenu } ;
  179.  
  180. END. { DummyUnit }
  181.  
  182.  
  183.